package net.sunofbeach.blog.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sunofbeach.blog.pojo.Label;
import net.sunofbeach.blog.pojo.query.LabelQuery;
import net.sunofbeach.blog.response.Result;
import net.sunofbeach.blog.service.LabelService;
import net.sunofbeach.blog.utils.TextUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Bing
 * @since 2021-11-24
 */
@Api(tags = "标签管理")
@RestController
@RequestMapping("/label")
public class LabelAdminApi {

    @Resource
    private LabelService labelService;

    /**
     * 增
     *
     * @param label
     * @return
     */
    @ApiOperation("新增标签")
    @PostMapping
    public Result addLabel(@RequestBody Label label) {
        // TODO 判断数据是否有效
        // 补全数据
        label.setCreateTime(LocalDateTime.now());
        label.setUpdateTime(LocalDateTime.now());
        labelService.save(label);

        return Result.ok("添加成功");
    }


    /**
     * 删
     *
     * @param labelId
     * @return
     */
    @ApiOperation("根据ID删除标签")
    @DeleteMapping("/{labelId}")
    public Result deleteLabel(@PathVariable("labelId") String labelId) {
        // TODO 判断数据是否有效
        if (TextUtils.isEmpty(labelId)) {
            return Result.fail("ID不能为空");
        }
        boolean count = labelService.removeById(labelId);
        if (count) {
            return Result.ok("删除成功");
        }
        return Result.fail("标签不存在");
    }

    /**
     * 改
     *
     * @param label
     * @return
     */
    @ApiOperation("根据ID更新标签")
    @PutMapping
    public Result updateLabel(@RequestBody Label label) {
        // TODO 判断数据是否有效：不为空、是否相同
        Label oldLabel = labelService.getById(label);
        if (label.getName().equals(oldLabel.getName())) {
            return Result.fail("标签已存在");
        }
        label.setUpdateTime(LocalDateTime.now());
        boolean count = labelService.updateById(label);
        if (count) {
            return Result.ok("更新成功");
        }
        return Result.fail("更新失败");
    }


    /**
     * 查
     *
     * @param labelId
     * @return
     */
    @ApiOperation("根据ID查询标签")
    @GetMapping("/{labelId}")
    public Result getLabelById(@PathVariable("labelId") String labelId) {
        // TODO 判断数据是否有效，id 不为空
        if (TextUtils.isEmpty(labelId)) {
            return Result.fail("ID不能为空");
        }
        Label label = labelService.getById(labelId);
        if (label == null) {
            return Result.fail("标签不存在");
        }
        return Result.ok(label).message("查询成功");
    }


    /**
     * 条件分页查询标签
     * @param page
     * @param size
     * @param labelQuery
     * @return
     */
    @ApiOperation("条件分页查询标签列表")
    @GetMapping("/{page}/{size}")
    public Result listLabels(@PathVariable Long page,
                             @PathVariable Long size,
                             LabelQuery labelQuery) {

        Page<Label> pageParam = new Page<>(page, size);
        IPage<Label> pageModel = labelService.selectPage(pageParam,labelQuery);
        return Result.ok(pageModel);
    }


}

